/**
 * 动态路由
 */

const Koa = require('koa')
const router = require('koa-router')() //引入并实例化路由

// 实例化
const app = new Koa()

//路由配置
router.get('/', async (ctx) => {
  ctx.body = '首页'
})
router.get('/news', async (ctx) => {
  ctx.body = '新闻页面' 
})

//动态路由 http://localhost:3000/newscontent/abc
router.get('/newscontent/:aid', async (ctx) => {
  
  // 获取动态路由的传值
  console.log(ctx.params) // { aid: 'abc' }

  ctx.body = '新闻详情' 
})
//动态路由可以传多个值
router.get('/newscontent/:aid/:bid', async (ctx) => {
  
  // 获取动态路由的传值
  console.log(ctx.params) // { aid: 'abc', bid: '123' }

  ctx.body = '新闻详情' 
})

//启动路由
app.use(router.routes())
app.use(router.allowedMethods())

app.listen(3000)